package com.yxb.service.server.manager;

import com.allen.dao.GenericDao;
import com.allen.page.DataPage;
import com.yxb.service.common.domain.DeviceData;
import com.yxb.service.common.option.DeviceDataOption;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author
 */
@Component
public class DeviceDataManager {

    @Autowired
    private GenericDao dao;

    public boolean addObject(DeviceData  deviceData){
        deviceData.setCreateTime(LocalDateTime.now());
        deviceData.setUpdateTime(LocalDateTime.now());
        return dao.insertAndSetupId("DeviceDataMapper.insert", deviceData) >= 1 ? true :false;
    }

    public List<DeviceData> queryList(String deviceId){
        Map<String, Object> parameter = new HashMap<>(5);
        if ( StringUtils.isBlank(deviceId) ){
            return null;
        }
        parameter.put("deviceId", deviceId);
        return dao.queryList("DeviceDataMapper.selectByCondition", parameter);
    }


    public DeviceData queryOne(Integer dId){
        Map<String, Object> parameter = new HashMap<>(5);
        if ( dId == null ){
            return null;
        }
        parameter.put("id", dId);
        return dao.queryUnique("DeviceDataMapper.selectByPrimaryKey", parameter);
    }


    public boolean updateObject(DeviceData deviceData){
        deviceData.setUpdateTime(LocalDateTime.now());
        return dao.insertAndSetupId("DeviceDataMapper.updateByPrimaryKey", deviceData) >= 1 ? true :false;
    }

    public boolean updateByAccept(DeviceData deviceData){
        deviceData.setUpdateTime(LocalDateTime.now());
        return dao.insertAndSetupId("DeviceDataMapper.updateByAccept", deviceData) >= 1 ? true :false;
    }

    public DataPage<DeviceData> queryPage(DataPage<DeviceData> dataPage, DeviceDataOption option){
        Map<String, Object> parameter  = new HashMap<>(5);
        parseParameter(parameter, option);
        return dao.queryPage("DeviceDataMapper.countPage", "DeviceDataMapper.queryPage", parameter, dataPage);
    }

    public DataPage<DeviceData> queryDevicePage(DataPage<DeviceData> dataPage, DeviceDataOption option){
        Map<String, Object> parameter  = new HashMap<>(5);
        parseParameter(parameter, option);
        return dao.queryPage("DeviceDataMapper.countDevicePage", "DeviceDataMapper.queryDevicePage", parameter, dataPage);
    }


    public List<DeviceData> queryList(DeviceDataOption option){
        Map<String, Object> parameter  = new HashMap<>(5);
        parseParameter(parameter, option);
        return dao.queryList("DeviceDataMapper.selectByCondition", parameter);
    }


    private void parseParameter(Map<String, Object> parameter , DeviceDataOption option){
        if ( option.getIsAccept() != null){
            parameter.put("isAccept", option.getIsAccept());
        }

        if ( StringUtils.isNotBlank(option.getAcceptUserId()) ){
            parameter.put("acceptUserId", option.getAcceptUserId());
        }

        if ( StringUtils.isNotBlank(option.getDeviceProductId()) ){
            parameter.put("deviceProductId", option.getAcceptUserId());
        }

        if ( StringUtils.isNotBlank(option.getDeviceId()) ){
            parameter.put("deviceId", option.getDeviceId());
        }

        if ( option.getMsgCreatedTime() != null ){
            parameter.put("msgCreatedTime", option.getMsgCreatedTime());
        }

        if ( option.getDeviceIdList() != null && !option.getDeviceIdList().isEmpty() ){
            parameter.put("deviceIdList", option.getDeviceIdList());
        }
    }


}
